package ynu.edu.online_course.mapper;

import org.apache.ibatis.annotations.*;
import ynu.edu.online_course.entity.Comment;
import java.util.List;

@Mapper
public interface CommentMapper {

    // 根据课程ID查询评论
    @Select("SELECT * FROM comment WHERE course_id = #{courseId}")
    List<Comment> findByCourseId(@Param("courseId") Long courseId);

    @Select("SELECT * FROM comment WHERE user_id = #{userId}")
    List<Comment> findCommentsByUserId(@Param("userId") Long userId);

    @Select("SELECT * FROM comment WHERE user_id = #{userId} AND course_id = #{courseId}")
    List<Comment> findCommentsByUserAndCourse(@Param("userId") Long userId, @Param("courseId") Long courseId);

    // 插入评论
    @Insert("INSERT INTO comment (course_id, parent_id, content, user_id) " +
            "VALUES (#{course.id}, #{parentId}, #{content}, #{user.id})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertComment(Comment comment);


    // 查询子评论
    @Select("SELECT * FROM comment WHERE parent_id = #{parentId}")
    List<Comment> findSubComments(@Param("parentId") Long parentId);

    // 删除评论
    @Delete("DELETE FROM comment WHERE id = #{id}")
    int deleteUserComment(@Param("id") Long id);

    @Select("SELECT user_id FROM comment WHERE id = #{commentId}")
    Long findUserIdByCommentId(@Param("commentId") Long commentId);

}
